package com.example.androidclientcamera;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

	ImageView imgView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imgView = (ImageView) findViewById(R.id.imageView1);
		// Thread thread = new Thread(new ThreadConnect());
		// thread.start();
		new ConnectToServer().execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class ShowImageFromBackground extends AsyncTask<byte[], Void, byte[]> {

		@Override
		protected byte[] doInBackground(byte[]... params) {
			// TODO Auto-generated method stub
			return params[0];
		}

		@Override
		protected void onPostExecute(byte[] result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			System.out.println("Rendering image");
			if (result != null) {
				Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0,
						result.length);
				imgView.setImageBitmap(bitmap);
			}
		}

	}

	class ConnectToServer extends AsyncTask<Void, Integer, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			byte[] mybytearray2 = null;
			InetAddress serverAddr = null;
			Socket socket;

			try {
				serverAddr = InetAddress.getByName("10.0.0.2");
				socket = new Socket(serverAddr, 6000);
				Log.e("tag", "Connected to server...");
				// get file size
				InputStream inputStream = socket.getInputStream();
				PrintWriter outPrint = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);
				BufferedReader input = new BufferedReader(
						new InputStreamReader(inputStream));
				String command = "";
				while (true) {
					// Read command
					command = input.readLine();
					if (command.equalsIgnoreCase("stream")) {
						int fileSize = Integer.valueOf(input.readLine());
						System.out.println("File size: "
								+ String.valueOf(fileSize));
						mybytearray2 = new byte[fileSize];
						// Send command back
						outPrint.println("next");
						int bytesRead;
						int current = 0;
						// Read byte
						bytesRead = inputStream.read(mybytearray2, 0,
								mybytearray2.length);
						current = bytesRead;
						do {
							bytesRead = inputStream.read(mybytearray2, current,
									(mybytearray2.length - current));
							if (bytesRead >= 0)
								current += bytesRead;
							// System.out.println("Byte: "
							// + String.valueOf(current));
						} while (bytesRead > -1 && current < fileSize);

						// System.out.println("Read file complete");
						outPrint.println("read_file_complete");
						// Show image
						new ShowImageFromBackground().execute(mybytearray2);
					} else if (command.equalsIgnoreCase("off")) {
						inputStream.close();
						outPrint.close();
						input.close();
						socket.close();
						break;
					}
				}
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

	}
}
